package com.shujia.scala

object Demo10List {
  def main(args: Array[String]): Unit = {
    /**
      *
      * list  不唯一   有序（插入顺序）
      *
      */

    val list = List(1, 2, 3, 4, 5, 6, 7, 8)

    for (elem <- list) {
      println(elem)
    }

    list.foreach(println)

    //map  返回一个新的list
    list.map(i => i * 2).foreach(println)

    list.filter(i => i % 2 == 1).foreach(println)


    println("=" * 100)


    //都是返回一个新的集合
    println(list.head) //头
    println(list.last) //尾
    println(list.take(2))
    println(list.takeRight(2))
    println(list.takeWhile(i => i % 2 == 1))
    println(list.reverse) //反转
    println(list.mkString(",")) //和split功能相反
    println(list.sum) //求和
    println(list.count(i => true)) //统计数量
    println(list.max)
    println(list.min)


    //排序
    val list1 = List(1, 2, 3, 5, 23, 2, 12, 3, 21, 412, 31, 24, 123)

    //sortBy   指定排序字段
    list1.sortBy(i => -i).foreach(println)


    val scores = List(Score("张三", 89), Score("李四", 56), Score("王五", 90), Score("晓伟", 70))

    //对分数进行排序
    scores.sortBy(score => score.sco).foreach(println)


    //sortWith  指定排序规则
    list1.sortWith((i, j) => i < j).foreach(println)
    scores.sortWith((s1, s2) => s1.sco > s2.sco).foreach(println)


    println(list.drop(2))
    println(list)


    val scores1 = List(Score("张三", 89), Score("张三", 56), Score("王五", 90), Score("晓伟", 70))

    //groupBy 指定分组字段
    println(scores1.groupBy(score => score.name))


  }

  case class Score(name: String, sco: Int)

}


